package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.MenuService;

import java.util.List;
import java.util.Map;

/**
 * 包名:  com.iteitan.controller;
 * 作者:  Eitan
 * 日期:  2019/9/9 10:09
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 查找父菜单数据
     */
    @RequestMapping("/findParentMenu")
    public Result findParentMenu(){
        try {
            List<Map> mapList= menuService.findParentMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 添加菜单
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 菜单分页
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= menuService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @RequestMapping("/findMenuAll")
    public Result findMenuAll(){
        try {
            List<Menu> menuList = menuService.findMenuAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuList);
        } catch (Exception e){
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 根据id查询菜单
     * @param id
     * @return
     */
    @RequestMapping("/findByMenuId")
    public Result findByMenuId(Integer id){
        try {
            Menu menu = menuService.findByMenuId(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e){
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 编辑菜单
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }
}
